package com.fancy.learncenter.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonFinishResultBean;
import com.fancy.learncenter.bean.CartoonQuestionBankBean;
import com.fancy.learncenter.bean.CartoonQuestionIdsBean;
import com.fancy.learncenter.bean.CartoonQuestionIdsItemBean;
import com.fancy.learncenter.bean.QuestionVOsBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.JumpIntentKey;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.common.MAudioPlayer;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.FragPagerAdapter;
import com.fancy.learncenter.ui.callback.OkOrCancelImpl;
import com.fancy.learncenter.ui.fragment.FMCallBack;
import com.fancy.learncenter.ui.fragment.GrammarChooseFillBlankNewFragment;
import com.fancy.learncenter.ui.fragment.GrammarListenAndImageChooseTextNewFragment;
import com.fancy.learncenter.ui.fragment.GrammarListenChooseImageNewFragment;
import com.fancy.learncenter.ui.fragment.GrammarSeeImageChooseNewFragment;
import com.fancy.learncenter.ui.fragment.MyBaseFragment;
import com.fancy.learncenter.ui.fragment.SeeWordPuzzleNewFragment;
import com.fancy.learncenter.ui.fragment.WordFillBlankNewFragment;
import com.fancy.learncenter.ui.fragment.WordListenChooseImageNewFragment;
import com.fancy.learncenter.ui.fragment.WordSeeImageChooseWordNewFragment;
import com.fancy.learncenter.ui.fragment.WordSeeImageListenSpellNewFragment;
import com.fancy.learncenter.ui.view.CartoonDoWorkExitPopu;
import com.fancy.learncenter.utils.FileUtils;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

import static com.fancy.learncenter.common.JumpIntentKey.RIGHT_NUM;

/**
 * 作业题型
 */
public class CartoonWorkQuestionTypeActivity extends BaseActivity implements
        GrammarListenChooseImageNewFragment.OnGrammarListenChooseListener,

        WordListenChooseImageNewFragment.OnListenChooseImageListener,

        SeeWordPuzzleNewFragment.OnSeeWordPuzzleListener,

        WordSeeImageChooseWordNewFragment.OnWordSeeImageChooseWordListener,

        WordSeeImageListenSpellNewFragment.OnWordSeeImageListenSpellListener,

        GrammarChooseFillBlankNewFragment.OnGrammarChooseFillBlankListener,

        GrammarListenAndImageChooseTextNewFragment.OnListenChooseListener,

        GrammarSeeImageChooseNewFragment.OnSeeImageChooseListener,

        WordFillBlankNewFragment.OnFillBlankListener {


    public static long JUMP_FRG_TIME = 1000;
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.current_index_text)
    TextView currentIndexText;
    @Bind(R.id.right_layout)
    LinearLayout rightLayout;
    @Bind(R.id.progress_num)
    TextView progressNum;

    int totalViewWidth;
    @Bind(R.id.progress_num_layout)
    FrameLayout progressNumLayout;
    @Bind(R.id.total_num_text)
    TextView totalNumText;
    int totalNum;
    ArrayList<QuestionVOsBean> questionBankList = new ArrayList<>();
    Fragment fragment;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    @Bind(R.id.commit_image)
    ImageView commitImage;
    @Bind(R.id.previous_question)
    ImageView previousQuestion;
    @Bind(R.id.next_question)
    ImageView nextQuestion;
    @Bind(R.id.rootView)
    RelativeLayout rootView;
    @Bind(R.id.bottom_layout)
    RelativeLayout bottomLayout;
    @Bind(R.id.customProgressBar)
    ProgressBar customProgressBar;
    private JumpIntentKey.QUESTION_ENTER_TYPE type;
    private String homePackageId;
    private String studentHomeworkId;
    private String constructId;
    private Bundle bundle;
    private CartoonDoWorkExitPopu exitPopu;
    private int currentIndex;
    private String unitId;
    private String unitNameStr;
    private String lessonNameStr;
    Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCartoonContentView(R.layout.activity_work_question_type, Status.STATUS);
        ButterKnife.bind(this);
        getIntentData();
        getReceiveData();
        initData();


        nextPlayer = new MAudioPlayer(CartoonWorkQuestionTypeActivity.this, R.raw.cartoon_next);

        submitPlayer = new MAudioPlayer(CartoonWorkQuestionTypeActivity.this, R.raw.cartoon_submit);

    }

    public ImageView getCommitImage() {
        return commitImage;
    }

    MAudioPlayer nextPlayer;
    MAudioPlayer submitPlayer;

    private void initData() {

        switch (type) {
            case DO_WORK:
            case CONTINUE_WORK:
                getReceiveData();
                initView();
                currentIndexText.setText(String.valueOf(currentIndex) + "/");
                totalNumText.setText(String.valueOf(totalNum));
                break;
            case DO_IT_AGAIN:
                getNormalData();
                getQuestionListData();
                break;
            case HAS_CORRECTED:
                getData(type);
                break;
            case NOT_CORRECTED:
                getData(type);
                break;
            case ERROR_KINDS:
                getErrorKindsByIdData();
                break;
            case WORK_REPORT:
                getReceiveData();
                initView();
                break;
        }

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type != null) {
                    switch (type) {
                        case DO_WORK:
                        case CONTINUE_WORK:
                            showExitPopu();
                            break;
                        default:
                            finish();
                            break;
                    }
                }

            }
        });

    }

    public List<MyBaseFragment> fragmentDatas;


    public List<String> flagQus;

    private void getIntentData() {
        bundle = getIntent().getExtras();
        type = (JumpIntentKey.QUESTION_ENTER_TYPE) bundle.getSerializable(JumpIntentKey.QUESTION_ENTER);
        unitId = bundle.getString(JumpIntentKey.UNIT_ID);
        constructId = bundle.getString(JumpIntentKey.CONSTRUCTID);
    }

    private void getReceiveData() {
        questionBankList = bundle.getParcelableArrayList(JumpIntentKey.QUSETION_BANK_LIST_KEY);
        if (questionBankList != null) {
            totalNum = questionBankList.size();
        }

        getNormalData();
        lessonNameStr = bundle.getString(JumpIntentKey.LESSON_NAME);
        unitNameStr = bundle.getString(JumpIntentKey.UNIT_NAME);
        currentIndex = bundle.getInt(JumpIntentKey.CURRENT_INDEX, 0);
    }

    private void getNormalData() {
        homePackageId = bundle.getString(JumpIntentKey.HOMEWORK_PACKAGE_ID);
        studentHomeworkId = bundle.getString(JumpIntentKey.STUDENTHOMEWORKID);
    }

    MAudioPlayer finishPlayer;

    private void showExitPopu() {


        if (finishPlayer == null) {
            finishPlayer = new MAudioPlayer(CartoonWorkQuestionTypeActivity.this, R.raw.cartoon_finish);
        }
        finishPlayer.play();

        if (exitPopu == null) {
            exitPopu = new CartoonDoWorkExitPopu(this, back);
        }
        if (!exitPopu.isShowing()) {
            exitPopu.showPopupWindow();
        }

        exitPopu.setListener(new OkOrCancelImpl() {
            @Override
            public void ok() {
                RIGHT_NUM = 0;
                getRecordTime();
                finish();
            }

            @Override
            public void cancel() {

                exitPopu.dismiss();
            }
        });
    }

    /**
     * 获取开始时间,并将本次时间长度写入prefrerence中
     */
    private void getRecordTime() {

        long startTime = LoginUserSharePrefernce.getRecordStartTime(String.valueOf(homePackageId));
//        long startTime = 0;
//        if(!TextUtils.isEmpty(startTimeStr)){
//            startTime = Long.valueOf(startTimeStr);
//        }
        long endTime = System.currentTimeMillis();

        long timeDuration = endTime - startTime;
        long beforeTimeDuration = LoginUserSharePrefernce.getCartoonQuestionTime(homePackageId);
        LoginUserSharePrefernce.setCartoonQuestionTime(timeDuration + beforeTimeDuration, homePackageId);
        LoginUserSharePrefernce.setRecordStartTime(0, homePackageId);
    }

    private void getQuestionListData() {

        String fileName = Constant.CARTOON_QUESTION_BANK + String.valueOf(LoginUserSharePrefernce.getCustomId() + "_" + homePackageId) + ".td";
        downloadWork(fileName, studentHomeworkId);
//        if (!FileUtils.checKFileExists(Constant.CARTOON_QUESTION_BANK + fileName)) {
//
//            downloadWork(fileName, studentHomeworkId);
//
//        } else {
//            String readResultStr = FileUtils.readFromSD(fileName);
//
//            CartoonQuestionBankBean questionBankBean1 = gson.fromJson(readResultStr, CartoonQuestionBankBean.class);
//            questionBankList.addAll(questionBankBean1.getQuestionVOs());
//
//            if (questionBankList != null && questionBankList.size() > 0) {
//                totalNum = questionBankList.size();
//            }
//
//            initView();
//        }
    }


    /**
     * 下载作业包
     *
     * @param fileName
     * @param homeworkId
     */
    private void downloadWork(final String fileName, String homeworkId) {


        Map<String, Object> map = new HashMap<>();
        map.put("studentHomeworkId", String.valueOf(homeworkId));
        HttpMehtod.getInstance().cartoonDownloadHomeWorkById(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CartoonQuestionBankBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonQuestionBankBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonQuestionBankBean>() {
                    @Override
                    public void success(CartoonQuestionBankBean dataBean) {
//                        String str = gson.toJson(dataBean);
//                        FileUtils.creatFile(fileName);
//                        FileUtils.saveStringToFile(fileName, str);
//                        String readResultStr = FileUtils.readFromSD(fileName);
//                        CartoonQuestionBankBean questionBankBean1 = gson.fromJson(readResultStr, CartoonQuestionBankBean.class);
                        LogUtil.MyLog("CartoonQuestionBankBean", "====CartoonQuestionBankBean=" + dataBean.getQuestionVOs().size());
                        questionBankList = dataBean.getQuestionVOs();
                        if (questionBankList != null && questionBankList.size() > 0) {
                            totalNum = questionBankList.size();
                        }
                        initView();
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }
        });
    }

    /**
     * 错题中心 根据id
     */
    private void getErrorKindsByIdData() {
        questionBankList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("constructId", constructId);
        map.put("unitId", unitId);
        map.put("classId", MyApplication.classIDStr);
        HttpMehtod.getInstance().cartoonListConstructId(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                dataUtils(responseBodyResponse);
            }
        });

    }

    private void dataUtils(Response<ResponseBody> responseBodyResponse) {
        BaseBean<CartoonQuestionIdsBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonQuestionIdsBean.class);

        CustomNetCallBack callBack = new CustomNetCallBack(this, baseBean);
        callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonQuestionIdsBean>() {
            @Override
            public void success(CartoonQuestionIdsBean dataBean) {


                List<CartoonQuestionIdsItemBean> idsItemBeanList = dataBean.getObjectQuestionIds();
                if (idsItemBeanList != null && idsItemBeanList.size() > 0) {
                    String fileName = Constant.CARTOON_QUESTION_BANK + String.valueOf(LoginUserSharePrefernce.getCustomId() + "_" + homePackageId) + ".td";
                    if (!FileUtils.checKFileExists(fileName)) {
                        downLoadHomeworkByQids(idsItemBeanList);
                    } else {

                        Gson gson = new Gson();
                        String readResultStr = FileUtils.readFromSD(fileName);
                        CartoonQuestionBankBean questionBankBean1 = gson.fromJson(readResultStr, CartoonQuestionBankBean.class);
                        ArrayList<QuestionVOsBean> tempQuestionArrayList = new ArrayList<>();
                        tempQuestionArrayList.addAll(questionBankBean1.getQuestionVOs());

                        for (CartoonQuestionIdsItemBean itemBean : idsItemBeanList) {
                            for (QuestionVOsBean vOsBean : tempQuestionArrayList) {
                                if (String.valueOf(vOsBean.getQuestionId()).equals(itemBean.getObjectQuestionId())) {
                                    questionBankList.add(vOsBean);
                                    break;
                                }
                            }
                        }
//                        if (questionBankList.size() > 0) {
//                            homePackageId = questionBankList.get(0).getHomeworkPackageId();
//                            initView();
//                        } else {
//                            downLoadHomeworkByQids(idsItemBeanList);
//                        }
                        downLoadHomeworkByQids(idsItemBeanList);


                    }
                } else {
                    ToastUtil.show("  暂无错题");
                    finish();
                }

            }

            @Override
            public void fail(String errorCode) {

            }
        }).dealData();
    }


    /**
     * 获取已订正 未订正
     *
     * @param type
     */
    private void getData(JumpIntentKey.QUESTION_ENTER_TYPE type) {
        questionBankList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("classId", MyApplication.classIDStr);
        map.put("unitId", unitId);
        switch (type) {
            case NOT_CORRECTED:

                map.put("correctState", 1);
                break;

            case HAS_CORRECTED:
                map.put("correctState", 2);
                break;
        }

        HttpMehtod.getInstance().cartoonCorrectState(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                dataUtils(responseBodyResponse);
            }
        });
    }


    /**
     * 根据拼接的ids获取列表
     *
     * @param list
     */
    private void downLoadHomeworkByQids(final List<CartoonQuestionIdsItemBean> list) {
        String ids = "";
        for (int i = 0; i < list.size(); i++) {
            if (i != list.size() - 1) {
                ids += list.get(i).getObjectQuestionId() + ",";
            } else {
                ids += list.get(i).getObjectQuestionId();
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("qids", ids);
        HttpMehtod.getInstance().cartoonDownLoadHomeworkByQids(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                BaseBean<CartoonQuestionBankBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonQuestionBankBean.class);

                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonQuestionBankBean>() {
                    @Override
                    public void success(CartoonQuestionBankBean dataBean) {

                        questionBankList = (ArrayList<QuestionVOsBean>) dataBean.getQuestionVOs();
                        if (questionBankList != null) {
                            totalNum = questionBankList.size();

                            for (CartoonQuestionIdsItemBean itemBean : list) {

                                for (QuestionVOsBean bean : questionBankList) {
                                    if (String.valueOf(bean.getQuestionId()).equals(itemBean.getObjectQuestionId())) {


                                        bean.setStudentHomeworkDoneDetailId(itemBean.getStudentHomeworkDoneDetailId());

                                        break;
                                    }
                                }
                            }
                            homePackageId = dataBean.getQuestionVOs().get(0).getHomeworkPackageId();
                            initView();
                        }
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();


            }
        });
    }


    @Override
    public void onBackPressed() {
        if (type != null) {
            switch (type) {
                case DO_WORK:
                case CONTINUE_WORK:
                    showExitPopu();
                    break;
                default:
                    super.onBackPressed();
                    break;
            }
        } else {
            super.onBackPressed();
        }
    }


    private void initView() {
//        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//        Utils.switchFragment(this, transaction, questionBankList, currentIndex, R.id.fg_container, type, homePackageId, studentHomeworkId, lessonNameStr, unitNameStr);


        fragmentDatas = Utils.getFragment(this, questionBankList, currentIndex, type, homePackageId, studentHomeworkId, lessonNameStr, unitNameStr);
//        List<MyBaseFragment> fragmentDatas2 = new ArrayList<>();
//        fragmentDatas2.add(fragmentDatas.get(0));
//
//        fragmentDatas.removeAll(fragmentDatas);
//
//        fragmentDatas=fragmentDatas2;

        if (type == JumpIntentKey.QUESTION_ENTER_TYPE.DO_IT_AGAIN) {
            flagQus = new ArrayList<>();
            for (int i = 0; i < fragmentDatas.size(); i++) {
                flagQus.add(FMCallBack.DO_SUBMMIT);
            }

        }


        mFragmentAdapteradapter = new FragPagerAdapter(getSupportFragmentManager(), fragmentDatas);
        //给ViewPager设置适配器
        viewPager.setAdapter(mFragmentAdapteradapter);
        currentIndexText.setText("1/");
        if (currentIndex != 0) {
            viewPager.setCurrentItem(currentIndex);
            currentIndexText.setText((currentIndex + 1) + "/");
        }

        totalNumText.setText(fragmentDatas.size() + "");


        LogUtil.MyLog("onPageSelected", CartoonWorkQuestionTypeActivity.this.fragmentDatas.size() + "=asdsa==onPageSelected==");

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {


                MyBaseFragment myBaseFragment = fragmentDatas.get(position);

//                if (type == JumpIntentKey.QUESTION_ENTER_TYPE.DO_IT_AGAIN) {
//                    commitImage.setImageResource(R.mipmap.commit_gray);
//                    commitImage.setClickable(false);
//                } else {
//
//                }

                if (type != JumpIntentKey.QUESTION_ENTER_TYPE.WORK_REPORT) {
                    if (FMCallBack.DO_SUBMMIT.equals(myBaseFragment.fmCallBackValue())) {
                        commitImage.setImageResource(R.mipmap.commit_is_submit);
                        commitImage.setClickable(false);
                    } else {
                        commitImage.setImageResource(R.mipmap.commit_gray);
                        commitImage.setClickable(false);
                    }
                }


                if (position == fragmentDatas.size() - 1) {
                    finishWork();
                }

                currentPosition = viewPager.getCurrentItem();

                notifyData(viewPager.getCurrentItem(), fragmentDatas.size(), myBaseFragment.fmCallBackName());


            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });


    }


    public int currentPosition = 0;

    FragPagerAdapter mFragmentAdapteradapter;


    private void notifyData(final int currentIndex, final int totalNum, String title) {
        progressNumLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            boolean isFirst = true;

            @Override
            public void onGlobalLayout() {

                if (isFirst) {
                    isFirst = false;
                    totalViewWidth = progressNumLayout.getMeasuredWidth();

                    int width = totalViewWidth * (currentIndex + 1) / totalNum;

                    customProgressBar.setProgress(100 * (currentIndex + 1) / totalNum);

                    progressNum.setWidth(width);

                }
            }
        });
        currentIndexText.setText((currentIndex + 1) + "/");
        totalNumText.setText(totalNum + "");
        titleText.setText(title);
    }

    @OnClick({R.id.previous_question, R.id.commit_image, R.id.next_question})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.previous_question:
                viewPager.setCurrentItem(viewPager.getCurrentItem() - 1, true);
                nextPlayer.play();
                break;
            case R.id.commit_image:
                LogUtil.MyLog("commit_image", "========");

                MyBaseFragment baseFragment = (MyBaseFragment) mFragmentAdapteradapter.currentFragment;
                baseFragment.fmCallBack();

                break;
            case R.id.next_question:
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
                nextPlayer.play();
                break;
        }
    }

    //听音选图
    @Override
    public void onListenChooseImage(boolean isDo, int totalNum, String title) {

        LogUtil.MyLog("onListenChooseImage", "===isDo==false===");

        if (isDo) {

            if ((totalNum + "").equals(FMCallBack.DO_SUBMMIT)) {
                commitImage.setImageResource(R.mipmap.commit_is_submit);

                if (type != JumpIntentKey.QUESTION_ENTER_TYPE.WORK_REPORT) {
                    commitImage.setClickable(false);
                }


                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);

                if (viewPager.getCurrentItem() == fragmentDatas.size() - 1) {
                    finishWork();
                }

            } else {
                commitImage.setImageResource(R.mipmap.commit_light);
                commitImage.setClickable(true);
            }

        } else {

            commitImage.setImageResource(R.mipmap.commit_gray);

            if (type != JumpIntentKey.QUESTION_ENTER_TYPE.WORK_REPORT) {
                commitImage.setClickable(false);
            }


            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
        }

    }

    @Override
    public void onGrammarListenChoose(boolean isDo, int totalNum, String title) {
        if (isDo) {

            if ((totalNum + "").equals(FMCallBack.DO_SUBMMIT)) {
                commitImage.setImageResource(R.mipmap.commit_is_submit);
                commitImage.setClickable(false);

                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);

                if (viewPager.getCurrentItem() == fragmentDatas.size() - 1) {
                    finishWork();
                }

            } else {
                commitImage.setImageResource(R.mipmap.commit_light);
                commitImage.setClickable(true);
            }

        } else {
            commitImage.setImageResource(R.mipmap.commit_gray);
            commitImage.setClickable(false);
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
        }
    }

    @Override
    public void onListenChoose(boolean isDo, int totalNum, String title) {
        if (isDo) {

            if ((totalNum + "").equals(FMCallBack.DO_SUBMMIT)) {
                commitImage.setImageResource(R.mipmap.commit_is_submit);
                commitImage.setClickable(false);

                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
                if (viewPager.getCurrentItem() == fragmentDatas.size() - 1) {
                    finishWork();
                }

            } else {
                commitImage.setImageResource(R.mipmap.commit_light);
                commitImage.setClickable(true);
            }

        } else {
            commitImage.setImageResource(R.mipmap.commit_gray);
            commitImage.setClickable(false);
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
        }
    }

    @Override
    public void onGrammarChooseFillBlank(boolean isDo, int totalNum, String title) {
        if (isDo) {

            if ((totalNum + "").equals(FMCallBack.DO_SUBMMIT)) {
                commitImage.setImageResource(R.mipmap.commit_is_submit);
                commitImage.setClickable(false);

                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);

                if (viewPager.getCurrentItem() == fragmentDatas.size() - 1) {
                    finishWork();
                }

            } else {
                commitImage.setImageResource(R.mipmap.commit_light);
                commitImage.setClickable(true);
            }

        } else {
            commitImage.setImageResource(R.mipmap.commit_gray);
            commitImage.setClickable(false);
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
        }
    }

    @Override
    public void onSeeWordPuzzle(boolean isDo, int totalNum, String title) {
        if (isDo) {

            if ((totalNum + "").equals(FMCallBack.DO_SUBMMIT)) {
                commitImage.setImageResource(R.mipmap.commit_is_submit);
                commitImage.setClickable(false);

                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);

                if (viewPager.getCurrentItem() == fragmentDatas.size() - 1) {
                    finishWork();
                }

            } else {
                commitImage.setImageResource(R.mipmap.commit_light);
                commitImage.setClickable(true);
            }

        } else {
            commitImage.setImageResource(R.mipmap.commit_gray);
            commitImage.setClickable(false);
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
        }
    }

    @Override
    public void onWordSeeImageChooseWord(boolean isDo, int totalNum, String title) {
        if (isDo) {

            if ((totalNum + "").equals(FMCallBack.DO_SUBMMIT)) {
                commitImage.setImageResource(R.mipmap.commit_is_submit);
                commitImage.setClickable(false);

                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);

                if (viewPager.getCurrentItem() == fragmentDatas.size() - 1) {
                    finishWork();
                }

            } else {
                commitImage.setImageResource(R.mipmap.commit_light);
                commitImage.setClickable(true);
            }

        } else {
            commitImage.setImageResource(R.mipmap.commit_gray);
            commitImage.setClickable(false);
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
        }
    }

    @Override
    public void onWordSeeImageListenSpell(boolean isDo, int totalNum, String title) {
        if (isDo) {

            if ((totalNum + "").equals(FMCallBack.DO_SUBMMIT)) {
                commitImage.setImageResource(R.mipmap.commit_is_submit);
                commitImage.setClickable(false);

                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);

                if (viewPager.getCurrentItem() == fragmentDatas.size() - 1) {
                    finishWork();
                }

            } else {
                commitImage.setImageResource(R.mipmap.commit_light);
                commitImage.setClickable(true);
            }

        } else {
            commitImage.setImageResource(R.mipmap.commit_gray);
            commitImage.setClickable(false);
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
        }
    }

    @Override
    public void onFillBlank(boolean isDo, int totalNum, String title) {
        if (isDo) {

            if ((totalNum + "").equals(FMCallBack.DO_SUBMMIT)) {
                commitImage.setImageResource(R.mipmap.commit_is_submit);
                commitImage.setClickable(false);

                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);

                if (viewPager.getCurrentItem() == fragmentDatas.size() - 1) {
                    finishWork();
                }

            } else {
                commitImage.setImageResource(R.mipmap.commit_light);
                commitImage.setClickable(true);
            }

        } else {
            commitImage.setImageResource(R.mipmap.commit_gray);
            commitImage.setClickable(false);
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
        }
    }

    @Override
    public void onSeeImageChoose(boolean isDo, int totalNum, String title) {
        if (isDo) {

            if ((totalNum + "").equals(FMCallBack.DO_SUBMMIT)) {
                commitImage.setImageResource(R.mipmap.commit_is_submit);
                commitImage.setClickable(false);

                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);

                if (viewPager.getCurrentItem() == fragmentDatas.size() - 1) {
                    finishWork();
                }

            } else {
                commitImage.setImageResource(R.mipmap.commit_light);
                commitImage.setClickable(true);
            }

        } else {
            commitImage.setImageResource(R.mipmap.commit_gray);
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
            commitImage.setClickable(false);
        }
    }

    /**
     * 完成作业调用接口
     */
    private void finishWork() {

        if (type == JumpIntentKey.QUESTION_ENTER_TYPE.DO_IT_AGAIN) {
            for (int i = 0; i < fragmentDatas.size(); i++) {
                if (fragmentDatas.get(i).fmCallBackValue().equals(FMCallBack.NO_DO_SUBMMIT)) {
                    return;
                }
            }
        } else if (type == JumpIntentKey.QUESTION_ENTER_TYPE.NOT_CORRECTED
                || type == JumpIntentKey.QUESTION_ENTER_TYPE.WORK_REPORT
                || type == JumpIntentKey.QUESTION_ENTER_TYPE.ERROR_KINDS
                || type == JumpIntentKey.QUESTION_ENTER_TYPE.HAS_CORRECTED) {
            return;
        }

        Map<String, Object> map = new HashMap<>();

        map.put("classId", MyApplication.classIDStr);
        map.put("homeworkPackageId", homePackageId);
        map.put("studentHomeworkId", studentHomeworkId);

        HttpMehtod.getInstance().cartoonCompleteHomeWorks(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CartoonFinishResultBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonFinishResultBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonFinishResultBean>() {
                    @Override
                    public void success(CartoonFinishResultBean dataBean) {

                        Bundle bundle2 = new Bundle();
                        bundle2.putSerializable(JumpIntentKey.QUESTION_ENTER, type);
//                      bundle2.putLong(JumpIntentKey.ANSWER_QUESTION_DURATION, getRecordTime());
                        bundle2.putString(JumpIntentKey.QUESTION_NUM, fragmentDatas.size() + "");
                        bundle2.putString(JumpIntentKey.HOMEWORK_PACKAGE_ID, homePackageId);
                        bundle2.putString(JumpIntentKey.STUDENTHOMEWORKID, studentHomeworkId);
                        Intent intentFinish = new Intent(getBaseContext(), CartoonFinishedActivity.class);
                        intentFinish.putExtras(bundle2);
                        startActivity(intentFinish);
                        finish();

                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }
        });

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}